package com.example.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.entity.Order;
import com.example.common.feign.OrderFeignClient;
import com.example.common.feign.PaymentFeignClient;
import com.example.common.result.R;
import com.example.user.entity.User;
import com.example.user.mapper.UserMapper;
import com.example.user.service.UserService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    private OrderFeignClient orderFeignClient;

    @Resource
    private PaymentFeignClient paymentFeignClient;
    
    @Override
    public User getUserByUsername(String username) {
        return getOne(new QueryWrapper<User>().eq("username", username));
    }

    @GlobalTransactional
    @Override
    public Boolean order() throws Exception {
        R<Order> order = orderFeignClient.createOrder(1L, new BigDecimal("100"));
        paymentFeignClient.pay(order.getData().getOrderNo(), 1L, new BigDecimal("100"));
        return true;
    }
}
